(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.user.listController', listController);
	listController.$inject = [ '$scope', '$q', 'Constants', 'gridService', 'modalService', 'layoutConfig', 'validatorConfig', 'authService',
			'userService', 'commonService' ];
	function listController($scope, $q, Constants, gridService, modalService, layoutConfig, validatorConfig, authService, userService, commonService) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("userList");
		ctrl.layoutConfig = layoutConfig;
		ctrl.user = {};
		
		ctrl.init = function() {
			userService.loadOptions().then(function(userOptions) {
				userService.initOptions(ctrl, userOptions);
			});
			ctrl.gridOptions = gridService.createGridOptions($scope, ctrl.layoutConfig.tableConfigs.table, paginationChanged, rowSelectionChanged, sortChanged);
			ctrl.gridOptions.data = 'vm.searchCriteria.list';
			ctrl.searchCriteria = ctrl.gridOptions.defaultSearchCriteria;
			ctrl.search();
		};

		ctrl.view = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			$scope.go('cda.user.view', ctrl.selectedRows[0]);
		};

		ctrl.create = function() {
			$scope.go('cda.user.create');
		};

		ctrl.update = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			$scope.go('cda.user.update', ctrl.selectedRows[0]);
		};

		ctrl.delete = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			$scope.go('cda.user.delete', ctrl.selectedRows[0]);
		};
		
		ctrl.changeUserStatusSubmit = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			modalService.openCustomDialogs({
				'dialogsModule' : 'cda.changeUserStatus',
				'params': ctrl.selectedRows[0],
				'size' : [400,200],
				'btnCallback' : {
					'ok' : function(userStatus) {
						if (!userStatus.userStatus) {
							modalService.openErrorDialogs({
								messages : [ 'js.info.user.userstatus-required' ]
							});
							return;
						}
						var userModel = {};
						userModel.userCode = ctrl.selectedRows[0].userCode;
						userModel.userStatus = userStatus.userStatus;
						return authService.changeUserStatusSubmit(userModel).then(submitSuccessful, submitFailed);
					}
				}
			});
		};
		
		ctrl.resetPasswordSubmit = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			modalService.openConfirmDialogs({
				'messages' : [ 'title.dialog.confirm.reset'],
				'btnCallback' : {
					'ok' : function() {
						authService.create(ctrl.user).then(function(response) {
							return authService.resetPasswordSubmit(ctrl.selectedRows[0]).then(changedSuccessful, submitFailed);
						});
					}
				}
			});
			
		};
		
		
		function changedSuccessful(response) {
			
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.resetPassword' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.user.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.user.list',
					'state' : 'cda.user.list',
					'params' : {}
				}]
			});
		}
		
		function submitSuccessful(response) {
		
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.user.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.user.list',
					'state' : 'cda.user.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		ctrl.search = function() {
			ctrl.searchCriteria.list = [];
			authService.getList(ctrl.searchCriteria).then(
			function(response) {
				angular.merge(ctrl.searchCriteria, response.data);
				gridService.updateGridOptions(ctrl.searchCriteria, ctrl.gridOptions);
			},angular.noop);
		};

		function paginationChanged(pageNum, pageSize) {
			ctrl.searchCriteria.pageNum = pageNum;
			ctrl.searchCriteria.pageSize = pageSize;
			ctrl.searchCriteria.list = [];
			ctrl.search();
		}

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
		}

		function sortChanged(sortColumns) {
			ctrl.searchCriteria.sortDirection = sortColumns[0] ? sortColumns[0].direction : '';
			ctrl.searchCriteria.sortBy = sortColumns[0] ? sortColumns[0].sortBy : '';
			ctrl.search();
		}
	}
})(angular);
